package com.software.loginregister.fragment.menudraftsfragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.software.loginregister.R;
import com.software.loginregister.adapter.NoteDraftsAdapter;
import com.software.loginregister.entity.NoteDraft;

import java.util.ArrayList;
import java.util.List;

public class PostDraftFragment extends Fragment {
    private RecyclerView recyclerView;
    private TextView tvEmpty;
    private TextView tvCount;
    private List<NoteDraft> noteDrafts;
    private NoteDraftsAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_post_drafts, container, false);

        initViews(view);
        initData();
        return view;
    }

    private void initViews(View view) {
        recyclerView = view.findViewById(R.id.rv_note_drafts);
        tvEmpty = view.findViewById(R.id.tv_empty);
        tvCount = view.findViewById(R.id.tv_count);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    private void initData() {
        noteDrafts = new ArrayList<>();
        adapter = new NoteDraftsAdapter(getContext(), noteDrafts);
        recyclerView.setAdapter(adapter);
        // 加载笔记草稿数据
        loadNoteDrafts();
    }

    private void loadNoteDrafts() {
        // TODO: 从数据库加载真实数据
        // 这里添加测试数据
        NoteDraft draft = new NoteDraft(1, "123", "image_url", System.currentTimeMillis());
        noteDrafts.add(draft);

        updateUi();
    }

    private void updateUi() {
        if (noteDrafts.isEmpty()) {
            tvEmpty.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            tvCount.setVisibility(View.GONE);
        } else {
            tvEmpty.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            tvCount.setVisibility(View.VISIBLE);
            tvCount.setText(String.valueOf(noteDrafts.size()));
        }
        adapter.notifyDataSetChanged();
    }
}